package com.foo.HistoricalData;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ejb.Stateless;
import com.foo.FileParser.FileParser;
import com.foo.TextDataSender.DataSender;

/**
 * Session Bean implementation class HistoricDataProvider
 */
@Stateless
public class HistoricDataProvider implements HistoricDataProviderRemote, HistoricDataProviderLocal {

	private FileParser objFileParser;
	
	DataSender datasender = new DataSender();
    /**
     * Default constructor. 
     */
    public HistoricDataProvider() {
        // TODO Auto-generated constructor stub
    }
    private Date convertToDate(String date)
    {
    	try
    	{
    	 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    	 Date convertedDate = (Date) dateFormat.parse(date);
    	 return convertedDate;
    	}
    	catch(Exception e)
    	{
    		System.out.println("Error while parsing!!!");
    		return null;
    	}

    }
	@Override
	public void sendHistoricData(String paramname, Date startDate, Date endDate) {
		objFileParser = new FileParser();
		try
		{
			String filePath = "data.txt";
			File file = new File(filePath);
			filePath = file.getAbsolutePath();
			int endIndex = file.getAbsolutePath().lastIndexOf(file.separator);
			String archivePath = filePath.substring(0,endIndex);
			File rootDir = new File(archivePath); //Checks the root directory of the JBoss for files
			
			System.out.println("Root Directory Path : " + rootDir.getAbsolutePath());
			
			StringBuilder fullData = new StringBuilder();

			String[] fileList = rootDir.list();
			for(String inputFile : fileList) 
			{
				if(inputFile.endsWith(".zip"))
				{
					System.out.println("Input file: " + inputFile);
					
					// Format YYYYMMDD-YYYYMMDD
					String[] str = inputFile.split("-");
					
					if(str != null)
					{
						System.out.println("Length of the String File Name: " + str.length);
						if(str.length == 2)
						{
							Date fileStart = convertToDate(str[0]);
							Date fileEnd = convertToDate(str[1]);
			
							if ((startDate.compareTo(fileStart) > 0 && startDate.compareTo(fileEnd) < 0) ||
									(endDate.compareTo(fileStart) > 0 && endDate.compareTo(fileEnd) <0)) {
								System.out.println("In Range: " + inputFile);
								String data = objFileParser.readData(inputFile, startDate, endDate, paramname);
								if(data !=  null)
								{
									fullData.append(objFileParser.readData(inputFile, startDate, endDate, paramname));
									fullData.append(",");
								}
							}
						}
					}
				}
			}
				String data = fullData.toString().replace(":", ",");
				data = data.replace(",,", ",");
				if(data.endsWith(","))
				{
					System.out.println("Previous The Historical message " +data);
					data = data.substring(0, data.length()-1);
				}
			System.out.println("The Historical message " +data);
			datasender.sendMessage("HistoricData",data);
		}
		catch(Exception e)
		{
			
		}
		
		
	}

}
